<template>
  <div class="messagelist">
    <!-- 头部搜索 -->
    <Input />
    <!-- 中间列表 -->
    <MessageList :userlist="newuserlist" />
  </div>
</template>

<script>
import Input from "@/components/message/Input";
import MessageList from "@/components/message/MessageList";
import { getAllnoread, getuserdata } from "@/api/data";
export default {
  data() {
    return {
      userlist: [],
      idlist: [], // 未读消息的好友列表id
    };
  },

  components: {
    Input,
    MessageList,
  },
  computed: {
    newuserlist() {
      if (
        (this.$store.state.newuser != null &&
          this.idlist.includes(this.$store.state.newuser._id)) ||
        (this.$store.state.olduser != null &&
          this.idlist.includes(this.$store.state.olduser._id))
      ) {
        // 上线
        if (this.$store.state.newuser != null) {
          console.log("lll");
          // 上线的新用户正好发送了未读消息,修改该用户的在线状态
          let index = this.idlist.findIndex((item) => {
            return item == this.$store.state.newuser._id;
          });

          this.userlist[index].from_id.isOnline = true;
        }
        // 下线
        if (this.$store.state.olduser != null) {
          console.log("222");
          // 原来在线的有未读消息的好友下线，修改该用户的在线状态
          let index = this.idlist.findIndex((item) => {
            return item == this.$store.state.olduser._id;
          });
          this.userlist[index].from_id.isOnline = false;
        }
        return this.userlist;
      } else if (this.$store.state.newmessage != null) {
        // 先依据新信息的id去获取发送方的具体信息
        getuserdata({ _id: this.$store.state.newmessage._id }).then((res) => {
          // console.log("有消息来了")
          // console.log(this.idlist)
          // console.log(this.$store.state.newmessage.from_id)
          if (res.code == 200) {
            // 新发送的消息在原来的列表里
            if (this.idlist.includes(this.$store.state.newmessage.from_id)) {
              console.log("33");
              let index = this.idlist.findIndex((item) => {
                return item == this.$store.state.newmessage.from_id;
              });

              this.userlist[index].noreadnumber++;
              this.$store.commit("removenewmessage");

              return this.userlist;
            }
            // 新发送的消息没在原来的列表里
            if (!this.idlist.includes(this.$store.state.newmessage.from_id)) {
              console.log("44");
              res.data.noreadnumber = 1;
              this.userlist.push(res.data);
              this.idlist.push(this.$store.state.newmessage.from_id)
              this.$store.commit("removenewmessage");
              return this.userlist;
            }
          }
        });
      } else {
        console.log("555")
        return this.userlist;
      }
    },
  },
  mounted() {
    // 获取当前用户的未读消息列表
    getAllnoread({ _id: this.$store.state.userdata._id }).then((res) => {
      if (res.code == 200) {
        this.userlist = res.data;

        this.idlist = this.userlist.map((item) => {
          return item.from_id._id;
        });
      }
    });

 
  },
};
</script>

<style scoped>
.messagelist {
  background-color: #fff;
  height: calc(100vh - 100px);
  overflow: scroll;
}
ul {
  height: 1500px;
  background-color: blue;
}
</style>